#include "dbManager.h"
#include "db.h"
#include "modelTable.h"
#include "productTable.h"
#include "protocolTable.h"
#include "modbusProtocolTable.h"
#include "linkageTable.h"
#include "gwInterface.h"
#include "terminalDeviceTable.h"
#include "taskTable.h"
#include "taskItemTable.h"
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>

#define DB_FILE_PATH  "../data/data.db"

int dbInit(){
    if(db_open(DB_FILE_PATH)){
        printf("open db failed\n");
         return -1;
    }
    return 0;
}

int dbManagerInit() {
    if(0 > dbInit()){
        return -1;
    }
    //初始化模型表
    dbModelInit();
    //初始化产品表
    dbProductInit();
    //初始化协议表
    dbProtocolInit();
    //初始化联动规则
    dbLinkageInit();
    //初始化modbus表
    dbModbusInit();
    //初始化网关接口表
    dbGWInterfaceInit();
    //初始话终端设备表
    dbTerminalDeviceInit();
    //初始化任务表
    dbTaskInit();
    //初始化任务项表
    dbTaskItemInit();
    // TODO: 实现数据库管理器的初始化逻辑
}
//=============================================================
// 以下为数据宏定义实现表的自动创建
#if 0
#define FL_INTERNAL_ARG_COUNT_PRIVATE(\
	 _0,  _1,  _2,  _3,  _4,  _5,  _6,  _7,  _8,  _9, \
	_10, _11, _12, _13, _14, _15, _16, _17, _18, _19, \
	_20, _21, _22, _23, _24, _25, _26, _27, _28, _29, \
	_30, _31, _32, _33, _34, _35, _36, _37, _38, _39, \
	_40, _41, _42, _43, _44, _45, _46, _47, _48, _49, \
	_50, _51, _52, _53, _54, _55, _56, _57, _58, _59, \
	_60, _61, _62, _63, _64, N, ...) N
	
// 计算 __VA_ARGS__ 参数个数,最大支持64个参数
#define FL_ARG_COUNT(...) FL_INTERNAL_ARG_COUNT_PRIVATE(0, ##__VA_ARGS__,\
	64, 63, 62, 61, 60, \
	59, 58, 57, 56, 55, 54, 53, 52, 51, 50, \
	49, 48, 47, 46, 45, 44, 43, 42, 41, 40, \
	39, 38, 37, 36, 35, 34, 33, 32, 31, 30, \
	29, 28, 27, 26, 25, 24, 23, 22, 21, 20, \
	19, 18, 17, 16, 15, 14, 13, 12, 11, 10, \
	 9,  8,  7,  6,  5,  4,  3,  2,  1,  0)



#define TABLE_FIELD(field, type) "" #field " " #type
#define FIELD_STR_EMPTY(field)  "" #field


#define GET_TABLE_FIELD(sql, field) { \
    if(0 < strlen(FIELD_STR_EMPTY(field))){ \
        if(__builtin_types_compatible_p(typeof(field),char) || __builtin_types_compatible_p(typeof(field),int8_t) || \
          __builtin_types_compatible_p(typeof(field),uint8_t) || __builtin_types_compatible_p(typeof(field),int16_t) || \
          __builtin_types_compatible_p(typeof(field),uint16_t) || __builtin_types_compatible_p(typeof(field),int32_t) || \
          __builtin_types_compatible_p(typeof(field),uint32_t) || __builtin_types_compatible_p(typeof(field), long) || \
          __builtin_types_compatible_p(typeof(field), unsigned long) || __builtin_types_compatible_p(typeof(field), int64_t) || \
          __builtin_types_compatible_p(typeof(field), uint64_t) || __builtin_types_compatible_p(typeof(field), bool)){\
            strcat(sql, ",");\
            strcat(sql, TABLE_FIELD(field, "INTEGER"));\
       }else if(__builtin_types_compatible_p(typeof(field),float) || __builtin_types_compatible_p(typeof(field),double)){\
            strcat(sql, ",");\
            strcat(sql, TABLE_FIELD(field, "REAL"));\
       }else if(__builtin_types_compatible_p(typeof(field),char*) || __builtin_types_compatible_p(typeof(field),char[])){\
            strcat(sql, ",");\
            strcat(sql, TABLE_FIELD(field, "TEXT"));\
       } \
   } \
}
#define GET_TABLE_FIRST_FIELD(sql,  field) { \
	if(0 < strlen(FIELD_STR_EMPTY(field))){ \
        if(__builtin_types_compatible_p(typeof(field),char) || __builtin_types_compatible_p(typeof(field),int8_t) || \
          __builtin_types_compatible_p(typeof(field),uint8_t) || __builtin_types_compatible_p(typeof(field),int16_t) || \
          __builtin_types_compatible_p(typeof(field),uint16_t) || __builtin_types_compatible_p(typeof(field),int32_t) || \
          __builtin_types_compatible_p(typeof(field),uint32_t) || __builtin_types_compatible_p(typeof(field), long) || \
          __builtin_types_compatible_p(typeof(field), unsigned long) || __builtin_types_compatible_p(typeof(field), int64_t) || \
          __builtin_types_compatible_p(typeof(field), uint64_t) || __builtin_types_compatible_p(typeof(field), bool)){\
            strcat(sql, TABLE_FIELD(field, "INTEGER"));\
       }else if(__builtin_types_compatible_p(typeof(field),float) || __builtin_types_compatible_p(typeof(field),double)){\
            strcat(sql, TABLE_FIELD(field, "REAL"));\
       }else if(__builtin_types_compatible_p(typeof(field),char*) || __builtin_types_compatible_p(typeof(field),char[])){\
            strcat(sql, TABLE_FIELD(field, "TEXT"));\
       } \
   } \
}

#define GET_FIELD_0(sql)  strcat(sql, ")");
#define GET_FIELD_1(sql, field, ...)  {GET_TABLE_FIELD(sql, field) GET_FIELD_0(sql)}
#define GET_FIELD_2(sql, field, ...)  {GET_TABLE_FIELD(sql, field) GET_FIELD_1(sql, ##__VA_ARGS__,  NULL)}
#define GET_FIELD_3(sql, field, ...)  {GET_TABLE_FIELD(sql, field) GET_FIELD_2(sql, ##__VA_ARGS__,  NULL)}
#define GET_FIELD_4(sql, field, ...)  {GET_TABLE_FIELD(sql, field) GET_FIELD_3(sql, ##__VA_ARGS__,  NULL)}
#define GET_FIELD_5(sql, field, ...)  {GET_TABLE_FIELD(sql, field) GET_FIELD_4(sql, ##__VA_ARGS__,  NULL)}
#define GET_FIELD_6(sql, field, ...)  {GET_TABLE_FIELD(sql, field) GET_FIELD_5(sql, ##__VA_ARGS__,  NULL)}
#define GET_FIELD_7(sql, field, ...)  {GET_TABLE_FIELD(sql, field) GET_FIELD_6(sql, ##__VA_ARGS__,  NULL)}
#define GET_FIELD_8(sql, field, ...)  {GET_TABLE_FIELD(sql, field) GET_FIELD_7(sql, ##__VA_ARGS__,  NULL)}
#define GET_FIELD_9(sql, field, ...)  {GET_TABLE_FIELD(sql, field) GET_FIELD_8(sql, ##__VA_ARGS__,  NULL)}
#define GET_FIELD_10(sql, field, ...) {GET_TABLE_FIELD(sql, field) GET_FIELD_9(sql, ##__VA_ARGS__,  NULL)}
#define GET_FIELD_11(sql, field, ...) {GET_TABLE_FIELD(sql, field) GET_FIELD_10(sql, ##__VA_ARGS__,  NULL)}
#define GET_FIELD_12(sql, field, ...) {GET_TABLE_FIELD(sql, field) GET_FIELD_11(sql, ##__VA_ARGS__,  NULL)}
#define GET_FIELD_13(sql, field, ...) {GET_TABLE_FIELD(sql, field) GET_FIELD_12(sql, ##__VA_ARGS__,  NULL)}
#define GET_FIELD_14(sql, field, ...) {GET_TABLE_FIELD(sql, field) GET_FIELD_13(sql, ##__VA_ARGS__,  NULL)}
#define GET_FIELD_15(sql, field, ...) {GET_TABLE_FIELD(sql, field) GET_FIELD_14(sql, ##__VA_ARGS__,  NULL)}
#define GET_FIELD_16(sql, field, ...) {GET_TABLE_FIELD(sql, field) GET_FIELD_15(sql, ##__VA_ARGS__,  NULL)}
#define GET_FIELD_17(sql, field, ...) {GET_TABLE_FIELD(sql, field) GET_FIELD_16(sql, ##__VA_ARGS__,  NULL)}
#define GET_FIELD_18(sql, field, ...) {GET_TABLE_FIELD(sql, field) GET_FIELD_17(sql, ##__VA_ARGS__,  NULL)}
#define GET_FIELD_19(sql, field, ...) {GET_TABLE_FIELD(sql, field) GET_FIELD_18(sql, ##__VA_ARGS__,  NULL)}
#define GET_FIELD_20(sql, field, ...) {GET_TABLE_FIELD(sql, field) GET_FIELD_19(sql, ##__VA_ARGS__,  NULL)}
#define GET_FIELD_21(sql, field, ...) {GET_TABLE_FIELD(sql, field) GET_FIELD_20(sql, ##__VA_ARGS__,  NULL)}
#define GET_FIELD_22(sql, field, ...) {GET_TABLE_FIELD(sql, field) GET_FIELD_21(sql, ##__VA_ARGS__,  NULL)}
#define GET_FIELD_23(sql, field, ...) {GET_TABLE_FIELD(sql, field) GET_FIELD_22(sql, ##__VA_ARGS__,  NULL)}
#define GET_FIELD_24(sql, field, ...) {GET_TABLE_FIELD(sql, field) GET_FIELD_23(sql, ##__VA_ARGS__,  NULL)}
#define GET_FIELD_25(sql, field, ...) {GET_TABLE_FIELD(sql, field) GET_FIELD_24(sql, ##__VA_ARGS__,  NULL)}
#define GET_FIELD_26(sql, field, ...) {GET_TABLE_FIELD(sql, field) GET_FIELD_25(sql, ##__VA_ARGS__,  NULL)}
#define GET_FIELD_27(sql, field, ...) {GET_TABLE_FIELD(sql, field) GET_FIELD_26(sql, ##__VA_ARGS__,  NULL)}
#define GET_FIELD_28(sql, field, ...) {GET_TABLE_FIELD(sql, field) GET_FIELD_27(sql, ##__VA_ARGS__,  NULL)}
#define GET_FIELD_29(sql, field, ...) {GET_TABLE_FIELD(sql, field) GET_FIELD_28(sql, ##__VA_ARGS__,  NULL)}
#define GET_FIELD_30(sql, field, ...) {GET_TABLE_FIELD(sql, field) GET_FIELD_29(sql, ##__VA_ARGS__,  NULL)}
#define GET_FIELD_31(sql, field, ...) {GET_TABLE_FIELD(sql, field) GET_FIELD_30(sql, ##__VA_ARGS__,  NULL)}
#define GET_FIELD_32(sql, field, ...) {GET_TABLE_FIELD(sql, field) GET_FIELD_31(sql, ##__VA_ARGS__,  NULL)}
#define GET_FIELD_33(sql, field, ...) {GET_TABLE_FIELD(sql, field) GET_FIELD_32(sql, ##__VA_ARGS__,  NULL)}
#define GET_FIELD_34(sql, field, ...) {GET_TABLE_FIELD(sql, field) GET_FIELD_33(sql, ##__VA_ARGS__,  NULL)}
#define GET_FIELD_35(sql, field, ...) {GET_TABLE_FIELD(sql, field) GET_FIELD_34(sql, ##__VA_ARGS__,  NULL)}
#define GET_FIELD_36(sql, field, ...) {GET_TABLE_FIELD(sql, field) GET_FIELD_35(sql, ##__VA_ARGS__,  NULL)}
#define GET_FIELD_37(sql, field, ...) {GET_TABLE_FIELD(sql, field) GET_FIELD_36(sql, ##__VA_ARGS__,  NULL)}
#define GET_FIELD_38(sql, field, ...) {GET_TABLE_FIELD(sql, field) GET_FIELD_37(sql, ##__VA_ARGS__,  NULL)}
#define GET_FIELD_39(sql, field, ...) {GET_TABLE_FIELD(sql, field) GET_FIELD_38(sql, ##__VA_ARGS__,  NULL)}
#define GET_FIELD_40(sql, field, ...) {GET_TABLE_FIELD(sql, field) GET_FIELD_39(sql, ##__VA_ARGS__,  NULL)}
#define GET_FIELD_41(sql, field, ...) {GET_TABLE_FIELD(sql, field) GET_FIELD_40(sql, ##__VA_ARGS__,  NULL)}
#define GET_FIELD_42(sql, field, ...) {GET_TABLE_FIELD(sql, field) GET_FIELD_41(sql, ##__VA_ARGS__,  NULL)}
#define GET_FIELD_43(sql, field, ...) {GET_TABLE_FIELD(sql, field) GET_FIELD_42(sql, ##__VA_ARGS__,  NULL)}
#define GET_FIELD_44(sql, field, ...) {GET_TABLE_FIELD(sql, field) GET_FIELD_43(sql, ##__VA_ARGS__,  NULL)}
#define GET_FIELD_45(sql, field, ...) {GET_TABLE_FIELD(sql, field) GET_FIELD_44(sql, ##__VA_ARGS__,  NULL)}
#define GET_FIELD_46(sql, field, ...) {GET_TABLE_FIELD(sql, field) GET_FIELD_45(sql, ##__VA_ARGS__,  NULL)}
#define GET_FIELD_47(sql, field, ...) {GET_TABLE_FIELD(sql, field) GET_FIELD_46(sql, ##__VA_ARGS__,  NULL)}
#define GET_FIELD_48(sql, field, ...) {GET_TABLE_FIELD(sql, field) GET_FIELD_47(sql, ##__VA_ARGS__,  NULL)}
#define GET_FIELD_49(sql, field, ...) {GET_TABLE_FIELD(sql, field) GET_FIELD_48(sql, ##__VA_ARGS__,  NULL)}
#define GET_FIELD_50(sql, field, ...) {GET_TABLE_FIELD(sql, field) GET_FIELD_49(sql, ##__VA_ARGS__,  NULL)}
#define GET_FIELD_51(sql, field, ...) {GET_TABLE_FIELD(sql, field) GET_FIELD_50(sql, ##__VA_ARGS__,  NULL)}
#define GET_FIELD_52(sql, field, ...) {GET_TABLE_FIELD(sql, field) GET_FIELD_51(sql, ##__VA_ARGS__,  NULL)}
#define GET_FIELD_53(sql, field, ...) {GET_TABLE_FIELD(sql, field) GET_FIELD_52(sql, ##__VA_ARGS__,  NULL)}
#define GET_FIELD_54(sql, field, ...) {GET_TABLE_FIELD(sql, field) GET_FIELD_53(sql, ##__VA_ARGS__,  NULL)}
#define GET_FIELD_55(sql, field, ...) {GET_TABLE_FIELD(sql, field) GET_FIELD_54(sql, ##__VA_ARGS__,  NULL)}
#define GET_FIELD_56(sql, field, ...) {GET_TABLE_FIELD(sql, field) GET_FIELD_55(sql, ##__VA_ARGS__,  NULL)}
#define GET_FIELD_57(sql, field, ...) {GET_TABLE_FIELD(sql, field) GET_FIELD_56(sql, ##__VA_ARGS__,  NULL)}
#define GET_FIELD_58(sql, field, ...) {GET_TABLE_FIELD(sql, field) GET_FIELD_57(sql, ##__VA_ARGS__,  NULL)}
#define GET_FIELD_59(sql, field, ...) {GET_TABLE_FIELD(sql, field) GET_FIELD_58(sql, ##__VA_ARGS__,  NULL)}
#define GET_FIELD_60(sql, field, ...) {GET_TABLE_FIELD(sql, field) GET_FIELD_59(sql, ##__VA_ARGS__,  NULL)}
#define GET_FIELD_61(sql, field, ...) {GET_TABLE_FIELD(sql, field) GET_FIELD_60(sql, ##__VA_ARGS__,  NULL)}
#define GET_FIELD_62(sql, field, ...) {GET_TABLE_FIELD(sql, field) GET_FIELD_61(sql, ##__VA_ARGS__,  NULL)}
#define GET_FIELD_63(sql, field, ...) {GET_TABLE_FIELD(sql, field) GET_FIELD_62(sql, ##__VA_ARGS__,  NULL)}
#define GET_FIELD_64(sql, field, ...) {GET_TABLE_FIELD(sql, field) GET_FIELD_63(sql, ##__VA_ARGS__,  NULL)}

#define GET_TABLE_FIELD_STR(sql, field, ...) GET_TABLE_FIRST_FIELD(sql, field) { \
	printf("参数数量为：%d\n", FL_ARG_COUNT(__VA_ARGS__));\
    switch(FL_ARG_COUNT(__VA_ARGS__)){ \
        case 0: GET_FIELD_0(sql); break; \
        case 1: GET_FIELD_1(sql, ##__VA_ARGS__); break; \
        case 2: GET_FIELD_2(sql, ##__VA_ARGS__); break; \
        case 3: GET_FIELD_3(sql, ##__VA_ARGS__); break; \
        case 4: GET_FIELD_4(sql, ##__VA_ARGS__); break; \
        case 5: GET_FIELD_5(sql, ##__VA_ARGS__); break; \
        case 6: GET_FIELD_6(sql, ##__VA_ARGS__); break; \
        case 7: GET_FIELD_7(sql, ##__VA_ARGS__); break; \
        case 8: GET_FIELD_8(sql, ##__VA_ARGS__); break; \
        case 9: GET_FIELD_9(sql, ##__VA_ARGS__); break; \
        case 10: GET_FIELD_10(sql, ##__VA_ARGS__); break; \
        case 11: GET_FIELD_11(sql, ##__VA_ARGS__); break; \
        case 12: GET_FIELD_12(sql, ##__VA_ARGS__); break; \
        case 13: GET_FIELD_13(sql, ##__VA_ARGS__); break; \
        case 14: GET_FIELD_14(sql, ##__VA_ARGS__); break; \
        case 15: GET_FIELD_15(sql, ##__VA_ARGS__); break; \
        case 16: GET_FIELD_16(sql, ##__VA_ARGS__); break; \
        case 17: GET_FIELD_17(sql, ##__VA_ARGS__); break; \
        case 18: GET_FIELD_18(sql, ##__VA_ARGS__); break; \
        case 19: GET_FIELD_19(sql, ##__VA_ARGS__); break; \
        case 20: GET_FIELD_20(sql, ##__VA_ARGS__); break; \
        case 21: GET_FIELD_21(sql, ##__VA_ARGS__); break; \
        case 22: GET_FIELD_22(sql, ##__VA_ARGS__); break; \
        case 23: GET_FIELD_23(sql, ##__VA_ARGS__); break; \
        case 24: GET_FIELD_24(sql, ##__VA_ARGS__); break; \
        case 25: GET_FIELD_25(sql, ##__VA_ARGS__); break; \
        case 26: GET_FIELD_26(sql, ##__VA_ARGS__); break; \
        case 27: GET_FIELD_27(sql, ##__VA_ARGS__); break; \
        case 28: GET_FIELD_28(sql, ##__VA_ARGS__); break; \
        case 29: GET_FIELD_29(sql, ##__VA_ARGS__); break; \
        case 30: GET_FIELD_30(sql, ##__VA_ARGS__); break; \
        case 31: GET_FIELD_31(sql, ##__VA_ARGS__); break; \
        case 32: GET_FIELD_32(sql, ##__VA_ARGS__); break; \
        case 33: GET_FIELD_33(sql, ##__VA_ARGS__); break; \
        case 34: GET_FIELD_34(sql, ##__VA_ARGS__); break; \
        case 35: GET_FIELD_35(sql, ##__VA_ARGS__); break; \
        case 36: GET_FIELD_36(sql, ##__VA_ARGS__); break; \
        case 37: GET_FIELD_37(sql, ##__VA_ARGS__); break; \
        case 38: GET_FIELD_38(sql, ##__VA_ARGS__); break; \
        case 39: GET_FIELD_39(sql, ##__VA_ARGS__); break; \
        case 40: GET_FIELD_40(sql, ##__VA_ARGS__); break; \
        case 41: GET_FIELD_41(sql, ##__VA_ARGS__); break; \
        case 42: GET_FIELD_42(sql, ##__VA_ARGS__); break; \
        case 43: GET_FIELD_43(sql, ##__VA_ARGS__); break; \
        case 44: GET_FIELD_44(sql, ##__VA_ARGS__); break; \
        case 45: GET_FIELD_45(sql, ##__VA_ARGS__); break; \
        case 46: GET_FIELD_46(sql, ##__VA_ARGS__); break; \
        case 47: GET_FIELD_47(sql, ##__VA_ARGS__); break; \
        case 48: GET_FIELD_48(sql, ##__VA_ARGS__); break; \
        case 49: GET_FIELD_49(sql, ##__VA_ARGS__); break; \
        case 50: GET_FIELD_50(sql, ##__VA_ARGS__); break; \
        case 51: GET_FIELD_51(sql, ##__VA_ARGS__); break; \
        case 52: GET_FIELD_52(sql, ##__VA_ARGS__); break; \
        case 53: GET_FIELD_53(sql, ##__VA_ARGS__); break; \
        case 54: GET_FIELD_54(sql, ##__VA_ARGS__); break; \
        case 55: GET_FIELD_55(sql, ##__VA_ARGS__); break; \
        case 56: GET_FIELD_56(sql, ##__VA_ARGS__); break; \
        case 57: GET_FIELD_57(sql, ##__VA_ARGS__); break; \
        case 58: GET_FIELD_58(sql, ##__VA_ARGS__); break; \
        case 59: GET_FIELD_59(sql, ##__VA_ARGS__); break; \
        case 60: GET_FIELD_60(sql, ##__VA_ARGS__); break; \
        case 61: GET_FIELD_61(sql, ##__VA_ARGS__); break; \
        case 62: GET_FIELD_62(sql, ##__VA_ARGS__); break; \
        case 63: GET_FIELD_63(sql, ##__VA_ARGS__); break; \
        case 64: GET_FIELD_64(sql, ##__VA_ARGS__); break; \
        default: break; \
    } \
}  

#define GET_VAR_FIELD(sql, ...) GET_TABLE_FIELD_STR(sql, ##__VA_ARGS__)
#endif